﻿#region COPYRIGHT
//--------------------------------------------------------------------------------
// <copyright file="MenuStrip.cs" company="starLiGHT Entertainment Studios">
//    Copyright (c) 2011 
//       Roland Rosenkranz (Glatzemann@email.de)
// </copyright>
// <license>
//   This file is part of starLiGHT.GUI.
//
//   It is released under the Microsoft Public License (Ms-PL).
//
//   You should have received a copy of the Microsoft Public License
//   along with starLiGHT.GUI. If not, see http://sle.codeplex.com/license.
//    
//   ADDITIONAL (commercial) LICENSES for starLiGHT.GUI are available on request.
// </license>
// <version>
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev::                       $:  Revision of last commit *
// * $Author::                    $:  Author of last commit   *
// * $Date::                      $:  Date of last commit     *
// ************************************************************
// </version>
//--------------------------------------------------------------------------------
#endregion

namespace starLiGHT.GUI.Widgets
{
#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
#endregion

    public class MenuStrip : ToolStrip
    {
        #region Constructors
        public MenuStrip(GuiManager manager, string name)
            : base(manager, name)
        {
            this.Init();
        }

        public MenuStrip(GuiManager manager, string name, string layout)
            : base(manager, name, layout)
        {
            this.Init();
        }
        #endregion

        public override bool CanFocus
        {
            get 
            { 
                return true; 
            }
        }

        public bool MenuExpanded
        {
            get;
            private set;
        }

        public override void OnMouseDown(object sender, MouseEventArgs e)
        {
            this.MenuExpanded = true;
            base.OnMouseDown(sender, e);
        }

        public override void OnLostFocus(object sender, EventArgs e)
        {
            this.MenuExpanded = false;
            base.OnLostFocus(sender, e);
        }

        public override bool HitTest(Vector2 position)
        {
            return this.HitTest(position.X, position.Y);
        }

        public override bool HitTest(float x, float y)
        {
            RectangleF hitRect = RectangleF.Empty;

            foreach (ToolStripMenuItem tsi in Items)
            {
                hitRect = RectangleF.Union(hitRect, tsi.GetHitTestRectangle());
                if (this.MenuExpanded)
                {
                    foreach (ToolStripMenuItem subItem in tsi.DropDownItems)
                    {
                        hitRect = RectangleF.Union(hitRect, subItem.GetHitTestRectangle());
                    }
                }

                if (hitRect.X <= x &&
                   x <= hitRect.Right &&
                   hitRect.Top <= y &&
                   y <= hitRect.Bottom)
                {
                    return true;
                }
            }

            return false;
        }

        private void Init()
        {
            this.Items.ItemAdded += new EventHandler(this.Items_ItemAdded);
        }

        private void Items_ItemAdded(object sender, EventArgs e)
        {
            Vector2 offset = ClientRectangle.TopLeft;
            foreach (ToolStripItem tsi in (ToolStripItemCollection)sender)
            {
                tsi.Offset = offset;
                offset.X += tsi.Size.X + Layout.GetProperty("ItemSpacing").FloatValue;
            }
        }
    }
}
